# parse argument

architecture=$1
matrix_decompressed_file=$2
matrix_comp_file=$3
output_dir=$4
lorem_ipsum_file=$5

permutation_str=""

start_timestamp=$(date +%s)
end_timestamp=$(expr $(date +%s) + 1000)

# generate different encoding paramethers

str="
xrnlib-cli \
    --encoding-conf -compiled-param -password-str \"AlicePassword0#\" \
    --dump-encoding-param -encoding-param $output_dir/alice_encoding_param_file.xf
"
echo ""; echo $str; echo ""
eval $str

str="
xrnlib-cli \
    --encoding-conf -compiled-param -password-str \"BobPassword0#\" \
    --dump-encoding-param -encoding-param $output_dir/bob_encoding_param_file.xf
"
echo ""; echo $str; echo ""
eval $str

str="
xrnlib-cli \
    --encoding-conf -compiled-param -password-str \"OneTimePassword0#\" \
    --dump-encoding-param -encoding-param $output_dir/otp_encoding_param_file.xf
"
echo ""; echo $str; echo ""
eval $str

# convert datastructures for the encoding paramethers

str="
xrnlib-cli \
    --decoding-conf -compiled-param -no-password \
    --encoding-conf -encoding-param $output_dir/alice_encoding_param_file.xf -no-password \
    --decode-encode -to-decode $matrix_decompressed_file -to-encode $output_dir/alice_xoron_matrix.xf
"
echo ""; echo $str; echo ""
eval $str

str="
xrnlib-cli \
    --decoding-conf -compiled-param -no-password \
    --encoding-conf -encoding-param $output_dir/alice_encoding_param_file.xf -no-password \
    --decode-encode -to-decode $matrix_comp_file -to-encode $output_dir/alice_xoron_matrix_compressed.xf
"
echo ""; echo $str; echo ""
eval $str

str="
xrnlib-cli \
    --decoding-conf -compiled-param -no-password \
    --encoding-conf -encoding-param $output_dir/bob_encoding_param_file.xf -no-password \
    --decode-encode -to-decode $matrix_decompressed_file -to-encode $output_dir/bob_xoron_matrix.xf
"
echo ""; echo $str; echo ""
eval $str

str="
xrnlib-cli \
    --decoding-conf -compiled-param -no-password \
    --encoding-conf -encoding-param $output_dir/bob_encoding_param_file.xf -no-password \
    --decode-encode -to-decode $matrix_comp_file -to-encode $output_dir/bob_xoron_matrix_compressed.xf
"
echo ""; echo $str; echo ""
eval $str

# generate alice data structures

str="
xrnlib-cli \
    --decoding-conf -encoding-param $output_dir/alice_encoding_param_file.xf -no-password \
    --encoding-conf -encoding-param $output_dir/alice_encoding_param_file.xf -no-password \
    --sequence-conf -xmatrix $output_dir/alice_xoron_matrix.xf \
    --permutation-conf -arch $architecture $permutation_str \
    --make-monovalent-key -monovalent-key $output_dir/alice_monovalent_key_file.xf -polyvalent-key $output_dir/alice_polyvalent_key_file.xf -polyvalent-proof $output_dir/alice_polyvalent_proof_file.xf -start-timestamp $start_timestamp -end-timestamp  $end_timestamp
"
echo ""; echo $str; echo ""
eval $str

# send the file to bob and extracting meta data

str="
xrnlib-cli \
    --decoding-conf -encoding-param $output_dir/alice_encoding_param_file.xf -no-password \
    --encoding-conf -encoding-param $output_dir/otp_encoding_param_file.xf -no-password \
    --decode-encode -to-decode $output_dir/alice_monovalent_key_file.xf -to-encode $output_dir/otp_alice_monovalent_key_file.xf
"
echo ""; echo $str; echo ""
eval $str

str="
xrnlib-cli \
    --decoding-conf -encoding-param $output_dir/otp_encoding_param_file.xf -no-password \
    --encoding-conf -encoding-param $output_dir/bob_encoding_param_file.xf -no-password \
    --decode-encode -to-decode $output_dir/otp_alice_monovalent_key_file.xf -to-encode $output_dir/alice_monovalent_key_file_for_bob.xf
"
echo ""; echo $str; echo ""
eval $str

str="
xrnlib-cli \
    --decoding-conf -encoding-param $output_dir/bob_encoding_param_file.xf -no-password \
    --permutation-conf -arch 128 \
    --logging-conf -lib-error-log $output_dir/print_alice_monovalent_key.err -lib-info-log $output_dir/print_alice_monovalent_key.info \
    --print-header -xrn-file $output_dir/alice_monovalent_key_file_for_bob.xf ; \
if grep -q XRN-ERR \"$output_dir/print_alice_monovalent_key.err\" ; then \
    rm $output_dir/print_alice_monovalent_key.err ; \
    rm $output_dir/print_alice_monovalent_key.info ; \
    xrnlib-cli \
        --decoding-conf -encoding-param $output_dir/bob_encoding_param_file.xf -no-password \
        --permutation-conf -arch 256 \
        --logging-conf -lib-error-log $output_dir/print_alice_monovalent_key.err -lib-info-log $output_dir/print_alice_monovalent_key.info \
        --print-header -xrn-file $output_dir/alice_monovalent_key_file_for_bob.xf ; \
fi
"
echo ""; echo $str; echo ""
eval $str

bob_architecture=$(cat $output_dir/print_alice_monovalent_key.info | grep nbits | sed 's/^.*:.// ; s/.$//')
bob_permutation_used=$(cat $output_dir/print_alice_monovalent_key.info | grep permutation_used | sed 's/^.*:.//')

bob_permutation_str=""

echo "# bob_architecture $bob_architecture bob_permutation_used $bob_permutation_used bob_permutation_str=$bob_permutation_str"

# bob generating monovalent key

str="
xrnlib-cli \
    --decoding-conf -encoding-param $output_dir/bob_encoding_param_file.xf -no-password \
    --encoding-conf -encoding-param $output_dir/bob_encoding_param_file.xf -no-password \
    --sequence-conf -xmatrix $output_dir/bob_xoron_matrix.xf \
    --permutation-conf -arch $architecture $permutation_str \
    --make-monovalent-key -monovalent-key $output_dir/bob_monovalent_key_file.xf -polyvalent-key $output_dir/bob_polyvalent_key_file.xf -polyvalent-proof $output_dir/bob_polyvalent_proof_file.xf -start-timestamp $start_timestamp -end-timestamp $end_timestamp
"
echo ""; echo $str; echo ""
eval $str

# exchange key for alice

str="
xrnlib-cli \
    --decoding-conf -encoding-param $output_dir/bob_encoding_param_file.xf -no-password \
    --encoding-conf -encoding-param $output_dir/otp_encoding_param_file.xf -no-password \
    --decode-encode -to-decode $output_dir/bob_monovalent_key_file.xf -to-encode $output_dir/otp_bob_monovalent_key_file.xf
"
echo ""; echo $str; echo ""
eval $str

str="
xrnlib-cli \
    --decoding-conf -encoding-param $output_dir/otp_encoding_param_file.xf -no-password \
    --encoding-conf -encoding-param $output_dir/alice_encoding_param_file.xf -no-password \
    --decode-encode -to-decode $output_dir/otp_bob_monovalent_key_file.xf -to-encode $output_dir/bob_monovalent_key_file_for_alice.xf
"
echo ""; echo $str; echo ""
eval $str

str="
xrnlib-cli \
    --decoding-conf -encoding-param $output_dir/alice_encoding_param_file.xf -no-password \
    --encoding-conf -encoding-param $output_dir/alice_encoding_param_file.xf -no-password \
    --arithmetic-conf -monovalent-key $output_dir/alice_monovalent_key_file.xf -polyvalent-key $output_dir/alice_polyvalent_key_file.xf -polyvalent-proof $output_dir/alice_polyvalent_proof_file.xf \
    --sequence-conf -xmatrix $output_dir/alice_xoron_matrix.xf \
    --permutation-conf -arch $architecture $permutation_str \
    --exchange-polyvalent-key -external-monovalent-key $output_dir/bob_monovalent_key_file_for_alice.xf -shared-polyvalent-key $output_dir/alice_shared_polyvalent_key_file.xf
"
echo ""; echo $str; echo ""
eval $str

# exchange key for bob

str="
xrnlib-cli \
    --decoding-conf -encoding-param $output_dir/bob_encoding_param_file.xf -no-password \
    --encoding-conf -encoding-param $output_dir/bob_encoding_param_file.xf -no-password \
    --arithmetic-conf -monovalent-key $output_dir/bob_monovalent_key_file.xf -polyvalent-key $output_dir/bob_polyvalent_key_file.xf -polyvalent-proof $output_dir/bob_polyvalent_proof_file.xf \
    --sequence-conf -xmatrix $output_dir/bob_xoron_matrix.xf \
    --permutation-conf -arch $bob_architecture $bob_permutation_str \
    --exchange-polyvalent-key -external-monovalent-key $output_dir/alice_monovalent_key_file_for_bob.xf -shared-polyvalent-key $output_dir/bob_shared_polyvalent_key_file.xf
"
echo ""; echo $str; echo ""
eval $str

# convert and compare shared keys to a plain text json files

str="
xrnlib-cli \
    --decoding-conf -encoding-param $output_dir/alice_encoding_param_file.xf -no-password \
    --permutation-conf -arch $architecture \
    --logging-conf -lib-info-log $output_dir/alice_shared_polyvalent_key_file.json \
    --print-header -xrn-file $output_dir/alice_shared_polyvalent_key_file.xf
"
echo ""; echo $str; echo ""
eval $str

str="
xrnlib-cli \
    --decoding-conf -encoding-param $output_dir/bob_encoding_param_file.xf -no-password \
    --permutation-conf -arch $bob_architecture \
    --logging-conf -lib-info-log $output_dir/bob_shared_polyvalent_key_file.json \
    --print-header -xrn-file $output_dir/bob_shared_polyvalent_key_file.xf
"
echo ""; echo $str; echo ""
eval $str

str=" 
cmp --silent $output_dir/alice_shared_polyvalent_key_file.json $output_dir/bob_shared_polyvalent_key_file.json || echo 'FAIL'
"
echo ""; echo $str; echo ""
eval $str


# convert key exchange to xmatrix exchange for alice


str="
xrnlib-cli \
    --decoding-conf -encoding-param $output_dir/alice_encoding_param_file.xf -no-password \
    --encoding-conf -encoding-param $output_dir/alice_encoding_param_file.xf -no-password \
    --sequence-conf -xmatrix $output_dir/alice_xoron_matrix.xf \
    --permutation-conf -arch $architecture $permutation_str \
    --make-hash -plain-text $output_dir/alice_shared_polyvalent_key_file.json -hash $output_dir/alice_hash_file.xf
"
echo ""; echo $str; echo ""
eval $str

str="
xrnlib-cli \
    --decoding-conf -encoding-param $output_dir/alice_encoding_param_file.xf -no-password \
    --encoding-conf -encoding-param $output_dir/alice_encoding_param_file.xf -no-password \
    --arithmetic-conf -hash $output_dir/alice_hash_file.xf \
    --permutation-conf -arch $architecture \
    --convert-hash-to-monomial-key -monomial-key $output_dir/alice_monomial_key_file.xf
"
echo ""; echo $str; echo ""
eval $str

str="
xrnlib-cli \
    --decoding-conf -encoding-param $output_dir/alice_encoding_param_file.xf -no-password \
    --encoding-conf -encoding-param $output_dir/alice_encoding_param_file.xf -no-password \
    --arithmetic-conf -monomial-key $output_dir/alice_monomial_key_file.xf \
    --sequence-conf -xmatrix-comp $output_dir/alice_xoron_matrix_compressed.xf -xmatrix $output_dir/alice_xoron_matrix.xf \
    --permutation-conf -arch $architecture \
    --permute-xmatrix-comp-confidential -xmatrix-comp-dst $output_dir/alice_xoron_matrix_compressed_shared.xf
"
echo ""; echo $str; echo ""
eval $str

str="
xrnlib-cli \
    --decoding-conf -encoding-param $output_dir/alice_encoding_param_file.xf -no-password \
    --encoding-conf -encoding-param $output_dir/alice_encoding_param_file.xf -no-password \
    --sequence-conf -xmatrix-comp $output_dir/alice_xoron_matrix_compressed_shared.xf \
    --permutation-conf -arch $architecture \
    --decompress-xmatrix -xmatrix $output_dir/alice_xoron_matrix_shared.xf
"
echo ""; echo $str; echo ""
eval $str

# convert key exchange to xmatrix exchange for bob

str="
xrnlib-cli \
    --decoding-conf -encoding-param $output_dir/bob_encoding_param_file.xf -no-password \
    --encoding-conf -encoding-param $output_dir/bob_encoding_param_file.xf -no-password \
    --sequence-conf -xmatrix $output_dir/bob_xoron_matrix.xf \
    --permutation-conf -arch $bob_architecture $bob_permutation_str \
    --make-hash -plain-text $output_dir/bob_shared_polyvalent_key_file.json -hash $output_dir/bob_hash_file.xf
"
echo ""; echo $str; echo ""
eval $str


str="
xrnlib-cli \
    --decoding-conf -encoding-param $output_dir/bob_encoding_param_file.xf -no-password \
    --encoding-conf -encoding-param $output_dir/bob_encoding_param_file.xf -no-password \
    --arithmetic-conf -hash $output_dir/bob_hash_file.xf \
    --permutation-conf -arch $bob_architecture \
    --convert-hash-to-monomial-key -monomial-key $output_dir/bob_monomial_key_file.xf
"
echo ""; echo $str; echo ""
eval $str

str="
xrnlib-cli \
    --decoding-conf -encoding-param $output_dir/bob_encoding_param_file.xf -no-password \
    --encoding-conf -encoding-param $output_dir/bob_encoding_param_file.xf -no-password \
    --arithmetic-conf -monomial-key $output_dir/bob_monomial_key_file.xf \
    --sequence-conf -xmatrix-comp $output_dir/bob_xoron_matrix_compressed.xf -xmatrix $output_dir/bob_xoron_matrix.xf \
    --permutation-conf -arch $architecture \
    --permute-xmatrix-comp-confidential -xmatrix-comp-dst $output_dir/bob_xoron_matrix_compressed_shared.xf
"
echo ""; echo $str; echo ""
eval $str

str="
xrnlib-cli \
    --decoding-conf -encoding-param $output_dir/bob_encoding_param_file.xf -no-password \
    --encoding-conf -encoding-param $output_dir/bob_encoding_param_file.xf -no-password \
    --sequence-conf -xmatrix-comp $output_dir/bob_xoron_matrix_compressed_shared.xf \
    --permutation-conf -arch $bob_architecture \
    --decompress-xmatrix -xmatrix $output_dir/bob_xoron_matrix_shared.xf
"
echo ""; echo $str; echo ""
eval $str

# convert and compare shared xoron matrices

str="
xrnlib-cli \
    --decoding-conf -encoding-param $output_dir/alice_encoding_param_file.xf -no-password \
    --permutation-conf -arch $architecture \
    --logging-conf -lib-info-log $output_dir/alice_xoron_matrix_compressed_shared.json \
    --print-header -xrn-file $output_dir/alice_xoron_matrix_compressed_shared.xf
"
echo ""; echo $str; echo ""
eval $str

str="
xrnlib-cli \
    --decoding-conf -encoding-param $output_dir/bob_encoding_param_file.xf -no-password \
    --permutation-conf -arch $bob_architecture \
    --logging-conf -lib-info-log $output_dir/bob_xoron_matrix_compressed_shared.json \
    --print-header -xrn-file $output_dir/bob_xoron_matrix_compressed_shared.xf
"
echo ""; echo $str; echo ""
eval $str


str=" 
cmp --silent $output_dir/bob_xoron_matrix_compressed_shared.json $output_dir/alice_xoron_matrix_compressed_shared.json || echo 'FAIL'
"
echo ""; echo $str; echo ""
eval $str

# alice and bob encrypts transfers the proofs

str="
xrnlib-cli \
    --decoding-conf -encoding-param $output_dir/alice_encoding_param_file.xf -no-password \
    --encoding-conf -encoding-param $output_dir/otp_encoding_param_file.xf -no-password \
    --decode-encode -to-decode $output_dir/alice_polyvalent_proof_file.xf -to-encode $output_dir/otp_alice_polyvalent_proof_file.xf
"

echo ""; echo $str; echo ""
eval $str


str="
xrnlib-cli \
    --decoding-conf -encoding-param $output_dir/alice_encoding_param_file.xf -no-password \
    --encoding-conf -encoding-param $output_dir/otp_encoding_param_file.xf -no-password \
    --arithmetic-conf -monomial-key $output_dir/alice_monomial_key_file.xf \
    --sequence-conf -xmatrix $output_dir/alice_xoron_matrix_shared.xf \
    --permutation-conf -arch $architecture \
    --encrypt-symmetric -plain-text $output_dir/otp_alice_polyvalent_proof_file.xf -cipher-text $output_dir/otp_alice_polyvalent_proof_file_encripted_to_bob.xf
"
echo ""; echo $str; echo ""
eval $str

str="
xrnlib-cli \
    --decoding-conf -encoding-param $output_dir/bob_encoding_param_file.xf -no-password \
    --encoding-conf -encoding-param $output_dir/otp_encoding_param_file.xf -no-password \
    --decode-encode -to-decode $output_dir/bob_polyvalent_proof_file.xf -to-encode $output_dir/otp_bob_polyvalent_proof_file.xf
"

echo ""; echo $str; echo ""
eval $str
str="
xrnlib-cli \
    --decoding-conf -encoding-param $output_dir/bob_encoding_param_file.xf -no-password \
    --encoding-conf -encoding-param $output_dir/otp_encoding_param_file.xf -no-password \
    --arithmetic-conf -monomial-key $output_dir/bob_monomial_key_file.xf \
    --sequence-conf -xmatrix $output_dir/bob_xoron_matrix_shared.xf \
    --permutation-conf -arch $bob_architecture \
    --encrypt-symmetric -plain-text $output_dir/otp_bob_polyvalent_proof_file.xf -cipher-text $output_dir/otp_bob_polyvalent_proof_file_encripted_to_alice.xf
"
echo ""; echo $str; echo ""
eval $str

# alice decrypts and check the monomial key of bob

str="
xrnlib-cli \
    --decoding-conf -encoding-param $output_dir/otp_encoding_param_file.xf -no-password \
    --encoding-conf -encoding-param $output_dir/alice_encoding_param_file.xf -no-password \
    --decode-encode -to-decode $output_dir/otp_bob_polyvalent_proof_file_encripted_to_alice.xf -to-encode $output_dir/alice_bob_polyvalent_proof_file_encripted_to_alice.xf
"
echo ""; echo $str; echo ""
eval $str

str="
xrnlib-cli \
    --decoding-conf -encoding-param $output_dir/alice_encoding_param_file.xf -no-password \
    --arithmetic-conf -monomial-key $output_dir/alice_monomial_key_file.xf \
    --sequence-conf -xmatrix $output_dir/alice_xoron_matrix_shared.xf \
    --permutation-conf -arch $architecture \
    --decipher-symmetric -plain-text $output_dir/otp_bob_polyvalent_proof_file_to_alice.xf -cipher-text $output_dir/alice_bob_polyvalent_proof_file_encripted_to_alice.xf
"
echo ""; echo $str; echo ""
eval $str

str="
xrnlib-cli \
    --decoding-conf -encoding-param $output_dir/otp_encoding_param_file.xf -no-password \
    --encoding-conf -encoding-param $output_dir/alice_encoding_param_file.xf -no-password \
    --decode-encode -to-decode $output_dir/otp_bob_polyvalent_proof_file_to_alice.xf -to-encode $output_dir/bob_polyvalent_proof_file_to_alice.xf
"
echo ""; echo $str; echo ""
eval $str

str="
xrnlib-cli \
    --decoding-conf -encoding-param $output_dir/alice_encoding_param_file.xf -no-password \
    --arithmetic-conf -monovalent-key $output_dir/bob_monovalent_key_file_for_alice.xf -polyvalent-proof $output_dir/bob_polyvalent_proof_file_to_alice.xf \
    --sequence-conf -xmatrix $output_dir/alice_xoron_matrix.xf \
    --permutation-conf -arch $architecture \
    --logging-conf -lib-info-log $output_dir/alice_info_log_check_monovalent_key.txt \
    --check-monovalent-key
"
echo ""; echo $str; echo ""
eval $str

str=" 
if ! [ -s $output_dir/alice_info_log_check_monovalent_key.txt ]; then echo FAIL ; exit ; fi ; \
if grep -q FAIL \"$output_dir/alice_info_log_check_monovalent_key.txt\" ; then echo FAIL ; exit ; fi
"
echo ""; echo $str; echo ""
eval $str

# bob decrypts and check the monomial key of alice

str="
xrnlib-cli \
    --decoding-conf -encoding-param $output_dir/otp_encoding_param_file.xf -no-password \
    --encoding-conf -encoding-param $output_dir/bob_encoding_param_file.xf -no-password \
    --decode-encode -to-decode $output_dir/otp_alice_polyvalent_proof_file_encripted_to_bob.xf -to-encode $output_dir/bob_alice_polyvalent_proof_file_encripted_to_bob.xf
"
echo ""; echo $str; echo ""
eval $str

str="
xrnlib-cli \
    --decoding-conf -encoding-param $output_dir/bob_encoding_param_file.xf -no-password \
    --arithmetic-conf -monomial-key $output_dir/bob_monomial_key_file.xf \
    --sequence-conf -xmatrix $output_dir/bob_xoron_matrix_shared.xf \
    --permutation-conf -arch $bob_architecture \
    --decipher-symmetric -plain-text $output_dir/otp_alice_polyvalent_proof_file_to_bob.xf -cipher-text $output_dir/bob_alice_polyvalent_proof_file_encripted_to_bob.xf
"
echo ""; echo $str; echo ""
eval $str

str="
xrnlib-cli \
    --decoding-conf -encoding-param $output_dir/otp_encoding_param_file.xf -no-password \
    --encoding-conf -encoding-param $output_dir/bob_encoding_param_file.xf -no-password \
    --decode-encode -to-decode $output_dir/otp_alice_polyvalent_proof_file_to_bob.xf -to-encode $output_dir/alice_polyvalent_proof_file_to_bob.xf
"
echo ""; echo $str; echo ""
eval $str

str="
xrnlib-cli \
    --decoding-conf -encoding-param $output_dir/bob_encoding_param_file.xf -no-password \
    --arithmetic-conf -monovalent-key $output_dir/alice_monovalent_key_file_for_bob.xf -polyvalent-proof $output_dir/alice_polyvalent_proof_file_to_bob.xf \
    --sequence-conf -xmatrix $output_dir/bob_xoron_matrix.xf \
    --permutation-conf -arch $bob_architecture \
    --logging-conf -lib-info-log $output_dir/bob_info_log_check_monovalent_key.txt \
    --check-monovalent-key
"
echo ""; echo $str; echo ""
eval $str

str=" 
if ! [ -s $output_dir/bob_info_log_check_monovalent_key.txt ]; then echo FAIL ; exit ; fi ; \
if grep -q FAIL \"$output_dir/bob_info_log_check_monovalent_key.txt\" ; then echo FAIL ; exit ; fi
"
echo ""; echo $str; echo ""
eval $str

